define(function (require, exports, module) {
    var $ = require("jquery");
    var template = require("template");
    
    var dialogModule = require('js/goToLogin');
    var tip = require('js/tip');
    var api_prefix = api_domain + "/api/index.php?r=cms/"; //api前缀
    var showSearchSuggestion = true;
    var Top = {
        init : function (){
            this.initEvents();
            showSearchSuggestion && this.getKeywords()            
                .then(function(res) {
                    if (res) {
                        var htmlBuf = [];
                        $.each(res,function(index,keyword){
                            htmlBuf.push('<span>'+ keyword + '</span>');
                        })
                        $('#keywords').html(htmlBuf.join(""))
                    }
                })
        },
        initEvents: function (){
            var self = this;
            var searchOpts = $('#searchOpts');
            $('#signout').click(function (){
                self.signout()
                    .then(function(res){
                        if (res.retCode == 0) {
                            location.href = "/";
                            return;
                        }
                        tip.showError(res.errMsg);
                    })
            });
            $("#searchBtn").click(function () {
               self.gotoSearch(); 
            });
            //回车搜索
            $("#keyword").on('keypress', function (evt) {
                if (evt.which == 13) {
                    self.gotoSearch(); 
                }
            })
            
            if (showSearchSuggestion) {
                $("#keyword").on('focus',function(e){
                    searchOpts.show();
                })
                .on('click',function(e){
                    e.stopPropagation()
                })
                searchOpts.on('click',function(e){
                    e.stopPropagation()
                    var target = $(e.target);
                    
                    if (target.is('span')) {
                        self.gotoSearch(target.html());
                    }
                });
                
                $('body').on('click',function(){
                    searchOpts.hide();
                })
            }    

        },
        getKeywords: function(){
            var url = api_prefix + "cms-data/get-common-tag&jsonp=" + jsopName;
            var defer = $.Deferred();
            $.getScript(url, function () {
                defer.resolve(window[jsopName]);
            });
            return defer.promise();
        },
        signout: function(){
            var url = api_prefix + "site/logout&jsonp=" + jsopName;
            var defer = $.Deferred();
            $.getScript(url, function () {
                defer.resolve(window[jsopName]);
            });
            return defer.promise();
        },
        gotoSearch: function(keyword){       
            location.href = '/search.html?keyword=' + encodeURIComponent(keyword || $("#keyword").val());
        }
    };
    
    Top.init();
    
});
